//
//  XQRadiusGradientView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQRadiusGradientView.h"

@interface XQRadiusGradientView ()

@property (strong, nonatomic) CALayer *contentLayer;

/** 圆角 */
@property (strong, nonatomic) CAShapeLayer *shaperLayer;
/** 渐变 */
@property (strong, nonatomic) CAGradientLayer *gradientLayer;
/** 边框 */
@property (strong, nonatomic) CAShapeLayer *borderLayer;
/** 阴影 */
@property (strong, nonatomic) CALayer *shadowLayer;

@end

@implementation XQRadiusGradientView

- (instancetype)init {
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _startPoint = CGPointMake(0, 0);
        _endPoint = CGPointMake(1, 0);
        _corner = UIRectCornerAllCorners;
        _locations = @[@0, @1];
        [self configureViews];
    }
    return self;
}

- (void)configureViews {
    self.backgroundColor = [UIColor clearColor];
    
    [self.layer addSublayer:self.shadowLayer];
    
    [self.layer addSublayer:self.contentLayer];
    
    self.contentLayer.mask = self.shaperLayer;
    [self.contentLayer addSublayer:self.gradientLayer];
    [self.contentLayer addSublayer:self.borderLayer];
}

#pragma mark - setter

- (void)setRadius:(CGFloat)radius {
    _radius = radius;
    [self setNeedsLayout];
}

- (void)setBgColor:(UIColor *)bgColor {
    _bgColor = bgColor;
    self.contentLayer.backgroundColor = bgColor.CGColor;
}

- (void)setColors:(NSArray *)colors {
    _colors = colors;
    self.gradientLayer.colors = _colors;
}

- (void)setStartPoint:(CGPoint)startPoint {
    _startPoint = startPoint;
    self.gradientLayer.startPoint = _startPoint;
}

- (void)setEndPoint:(CGPoint)endPoint {
    _endPoint = endPoint;
    self.gradientLayer.endPoint = _endPoint;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    _borderWidth = borderWidth;
    self.borderLayer.lineWidth = _borderWidth;
}

- (void)setBorderColor:(UIColor *)borderColor {
    _borderColor = borderColor;
    self.borderLayer.strokeColor = _borderColor.CGColor;
}

- (void)setCorner:(UIRectCorner)corner {
    _corner = corner;
    [self setNeedsLayout];
}

- (void)setLocations:(NSArray<NSNumber *> *)locations {
    _locations = locations;
    self.gradientLayer.locations = _locations;
}

- (void)setShadowColor:(UIColor *)shadowColor {
    _shadowColor = shadowColor;
    self.shadowLayer.shadowColor = _shadowColor.CGColor;
}

- (void)setShadowOffset:(CGSize)shadowOffset {
    _shadowOffset = shadowOffset;
    self.shadowLayer.shadowOffset = _shadowOffset;
}

- (void)setShadowRadius:(CGFloat)shadowRadius {
    _shadowRadius = shadowRadius;
    self.shadowLayer.shadowRadius = _shadowRadius;
}

- (void)setShadowOpacity:(CGFloat)shadowOpacity {
    _shadowOpacity = shadowOpacity;
    self.shadowLayer.shadowOpacity = _shadowOpacity;
}

#pragma mark - layoutSubviews

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.frame.size.width <= 0 || self.frame.size.height <= 0) {
        return;
    }
    self.contentLayer.frame = self.bounds;
    self.shaperLayer.frame = self.bounds;
    self.gradientLayer.frame = self.bounds;
    self.borderLayer.frame = self.bounds;
    
    if (self.radius > 0) {
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:self.corner cornerRadii:CGSizeMake(self.radius, self.radius)];
        self.shaperLayer.path = path.CGPath;
        self.borderLayer.path = path.CGPath;
        self.shadowLayer.shadowPath = path.CGPath;
    } else {
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
        self.shaperLayer.path = path.CGPath;
        self.borderLayer.path = path.CGPath;
        self.shadowLayer.shadowPath = path.CGPath;
    }
    
}

#pragma mark - getter

- (CALayer *)contentLayer {
    if (!_contentLayer) {
        _contentLayer = [CALayer layer];
    }
    return _contentLayer;
}

- (CAShapeLayer *)shaperLayer {
    if (!_shaperLayer) {
        _shaperLayer = [CAShapeLayer layer];
    }
    return _shaperLayer;
}

- (CAGradientLayer *)gradientLayer {
    if (!_gradientLayer) {
        _gradientLayer = [CAGradientLayer layer];
        _gradientLayer.startPoint = self.startPoint;
        _gradientLayer.endPoint = self.endPoint;
        _gradientLayer.locations = self.locations;
    }
    return _gradientLayer;
}

- (CAShapeLayer *)borderLayer {
    if (!_borderLayer) {
        _borderLayer = [CAShapeLayer layer];
        _borderLayer.fillColor = [UIColor clearColor].CGColor;
    }
    return _borderLayer;
}

- (CALayer *)shadowLayer {
    if (!_shadowLayer) {
        _shadowLayer = [CALayer layer];
    }
    return _shadowLayer;
}

@end
